package ren.yale.android.cachewebviewlib.config;

import android.text.TextUtils;

import java.util.HashSet;

/**
 * Created by yale on 2017/9/26.
 */

public class CacheExtensionConfig {
    //全局默认的
    private static final HashSet<String> STATIC = new HashSet<>();
    private static final HashSet<String> NO_CACHE = new HashSet<>();

    static {
        STATIC.add("html");
        STATIC.add("htm");
        STATIC.add("js");
        STATIC.add("ico");
        STATIC.add("css");
        STATIC.add("png");
        STATIC.add("jpg");
        STATIC.add("jpeg");
        STATIC.add("gif");
        STATIC.add("bmp");
        STATIC.add("ttf");
        STATIC.add("woff");
        STATIC.add("woff2");
        STATIC.add("otf");
        STATIC.add("eot");
        STATIC.add("svg");
        STATIC.add("xml");
        STATIC.add("swf");
        STATIC.add("txt");
        STATIC.add("text");
        STATIC.add("conf");
        STATIC.add("webp");

        NO_CACHE.add("mp4");
        NO_CACHE.add("mp3");
        NO_CACHE.add("ogg");
        NO_CACHE.add("avi");
        NO_CACHE.add("wmv");
        NO_CACHE.add("flv");
        NO_CACHE.add("rmvb");
        NO_CACHE.add("3gp");
    }

    //单独webview实例的
    private final HashSet<String> statics = new HashSet<>(STATIC);
    private final HashSet<String> noCache = new HashSet<>(NO_CACHE);

    public static void addGlobalExtension(String extension) {
        add(STATIC, extension);
    }

    public static void removeGlobalExtension(String extension) {
        remove(STATIC, extension);
    }

    private static void add(HashSet<String> set, String extension) {
        if (TextUtils.isEmpty(extension)) {
            return;
        }
        set.add(extension.replace(".", "").toLowerCase().trim());
    }

    private static void remove(HashSet<String> set, String extension) {
        if (TextUtils.isEmpty(extension)) {
            return;
        }
        set.remove(extension.replace(".", "").toLowerCase().trim());
    }

    public boolean isMedia(String extension) {
        if (TextUtils.isEmpty(extension)) {
            return false;
        }
        if (NO_CACHE.contains(extension)) {
            return true;
        }
        return noCache.contains(extension.toLowerCase().trim());
    }

    public boolean canCache(String extension) {

        if (TextUtils.isEmpty(extension)) {
            return false;
        }
        extension = extension.toLowerCase().trim();
        if (STATIC.contains(extension)) {
            return true;
        }
        return statics.contains(extension);

    }

    public CacheExtensionConfig addExtension(String extension) {
        add(statics, extension);
        return this;
    }

    public CacheExtensionConfig removeExtension(String extension) {
        remove(statics, extension);
        return this;
    }

    public boolean isHtml(String extension) {
        if (TextUtils.isEmpty(extension)) {
            return false;
        }
        if (extension.toLowerCase().contains("html") ||
                extension.toLowerCase().contains("htm")) {
            return true;
        }
        return false;
    }

    public void clearAll() {
        clearDiskExtension();
    }

    public void clearDiskExtension() {
        statics.clear();
    }

}
